Completed
Pull Request — develop (#209)
by Xaver
31s
created

language.js ➔ ... ➔ setLocale   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 4
rs 10
nop 1
1
define(['polyglot', 'moment', 'helper'], function (Polyglot, moment, helper) {
2
  'use strict';
3
  return function () {
4
    var router;
5
6
    function languageSelect(el) {
7
      var select = document.createElement('select');
8
      select.className = 'language-switch';
9
      select.setAttribute('aria-label', 'Language');
10
      select.addEventListener('change', setSelectLocale);
11
      el.appendChild(select);
12
13
      // Keep english
14
      select.innerHTML = '<option>Language</option>';
15
      for (var i = 0; i < config.supportedLocale.length; i++) {
0 ignored issues
show
Bug introduced by
The variable config seems to be never declared. If this is a global, consider adding a /** global: config */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
16
        select.innerHTML += '<option value="' + config.supportedLocale[i] + '">' + config.supportedLocale[i] + '</option>';
17
      }
18
    }
19
20
    function setSelectLocale(event) {
21
      router.fullUrl({ lang: event.target.value }, false, true);
22
    }
23
24
    function setLocale(lang) {
25
      localStorage.setItem('language', getLocale(lang));
0 ignored issues
show
Bug introduced by
The variable localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
26
      location.reload();
27
    }
28
29
    function getLocale(input) {
30
      var language = input || localStorage.getItem('language') || navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage;
0 ignored issues
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
31
      var locale = config.supportedLocale[0];
1 ignored issue
show
Bug introduced by
The variable config seems to be never declared. If this is a global, consider adding a /** global: config */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
32
      config.supportedLocale.some(function (item) {
33
        if (language.indexOf(item) !== -1) {
34
          locale = item;
35
          return true;
36
        }
37
        return false;
38
      });
39
      return locale;
40
    }
41
42
    function setTranslation(json) {
43
      _.extend(json);
44
45
      if (moment.locale(_.locale()) !== _.locale()) {
46
        moment.defineLocale(_.locale(), {
47
          longDateFormat: {
48
            LT: 'HH:mm',
49
            LTS: 'HH:mm:ss',
50
            L: 'DD.MM.YYYY',
51
            LL: 'D. MMMM YYYY',
52
            LLL: 'D. MMMM YYYY HH:mm',
53
            LLLL: 'dddd, D. MMMM YYYY HH:mm'
54
          },
55
          calendar: json.momentjs.calendar,
56
          relativeTime: json.momentjs.relativeTime
57
        });
58
      }
59
    }
60
61
    function init(r) {
62
      router = r;
63
      /** global: _ */
64
      window._ = new Polyglot({ locale: getLocale(router.getLang()), allowMissing: true });
65
      helper.getJSON('locale/' + _.locale() + '.json?' + config.cacheBreaker).then(setTranslation);
1 ignored issue
show
Bug introduced by
The variable config seems to be never declared. If this is a global, consider adding a /** global: config */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
66
      document.querySelector('html').setAttribute('lang', _.locale());
67
    }
68
69
    return {
70
      init: init,
71
      getLocale: getLocale,
72
      setLocale: setLocale,
73
      languageSelect: languageSelect
74
    };
75
  };
76
});
77